home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / BSEARCH.C < prev    next >
C/C++ Source or Header  |  1992-03-02  |  3KB  |  72 lines

  1. /* This file may have been modified by DJ Delorie (Jan 1991).  If so,
  2. ** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
  3. ** Rochester NH, 03867-2954, USA.
  4. */
  5.  
  6. /*
  7.  * Copyright (c) 1990 Regents of the University of California.
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms are permitted
  11.  * provided that: (1) source distributions retain this entire copyright
  12.  * notice and comment, and (2) distributions including binaries display
  13.  * the following acknowledgement:  ``This product includes software
  14.  * developed by the University of California, Berkeley and its contributors''
  15.  * in the documentation or other materials provided with the distribution
  16.  * and in all advertising materials mentioning features or use of this
  17.  * software. Neither the name of the University nor the names of its
  18.  * contributors may be used to endorse or promote products derived
  19.  * from this software without specific prior written permission.
  20.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  21.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  22.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  23.  */
  24.  
  25. #if defined(LIBC_SCCS) && !defined(lint)
  26. static char sccsid[] = "@(#)bsearch.c    5.3 (Berkeley) 5/17/90";
  27. #endif /* LIBC_SCCS and not lint */
  28.  
  29. #include <stddef.h>        /* size_t */
  30. #include <stdlib.h>
  31.  
  32. /*
  33.  * Perform a binary search.
  34.  *
  35.  * The code below is a bit sneaky.  After a comparison fails, we
  36.  * divide the work in half by moving either left or right. If lim
  37.  * is odd, moving left simply involves halving lim: e.g., when lim
  38.  * is 5 we look at item 2, so we change lim to 2 so that we will
  39.  * look at items 0 & 1.  If lim is even, the same applies.  If lim
  40.  * is odd, moving right again involes halving lim, this time moving
  41.  * the base up one item past p: e.g., when lim is 5 we change base
  42.  * to item 3 and make lim 2 so that we will look at items 3 and 4.
  43.  * If lim is even, however, we have to shrink it by one before
  44.  * halving: e.g., when lim is 4, we still looked at item 2, so we
  45.  * have to make lim 3, then halve, obtaining 1, so that we will only
  46.  * look at item 3.
  47.  */
  48. void *
  49. bsearch(key, base0, nmemb, size, compar)
  50.     register const void *key;
  51.     const void *base0;
  52.     unsigned long nmemb;
  53.     register unsigned long size;
  54.     register int (*compar)(const void *, const void *);
  55. {
  56.     register char *base = base0;
  57.     register int lim, cmp;
  58.     register void *p;
  59.  
  60.     for (lim = nmemb; lim != 0; lim >>= 1) {
  61.         p = base + (lim >> 1) * size;
  62.         cmp = (*compar)(key, p);
  63.         if (cmp == 0)
  64.             return (p);
  65.         if (cmp > 0) {    /* key > p: move right */
  66.             base = (char *)p + size;
  67.             lim--;
  68.         } /* else move left */
  69.     }
  70.     return (NULL);
  71. }
  72.